//
//  LKCashTradeViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/6/29.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKCashTradeViewController.h"
#import "XMTextField.h"
//#import "UITextField+CHTHealper.h"
#import <IQKeyboardManager.h>
#import "LKLockScanViewController.h"
#import "LKCustomKeyBoardView.h"
@interface LKCashTradeViewController ()<LKCustomKeyBoardViewDelegate>

@property(nonatomic,strong)UITextField *moneyTf;
@property(nonatomic,strong)UITextField *noteTf;

@end

@implementation LKCashTradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationRightBtn];
    self.navigationItem.title = @"收款";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
    UIView *moneyView = [self createHeaderViewWithTitle:@"请输入收款金额"];
    [self.view addSubview:moneyView];
    [moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT + 30);
        make.height.mas_equalTo(123);
    }];
    UIView *noteView = [self createNoteViewWithTitle:@"可输入财务系统订单号或其他描述"];
    [self.view addSubview:noteView];
    [noteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(moneyView.mas_bottom).offset(20);
        make.height.mas_equalTo(52);
    }];
    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"卡券核销" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        [weakSelf chooseErweima];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)chooseErweima{
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 3;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    style.isNeedShowRetangle = NO;
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"qrcode_scan_light_green"];
    style.animationImage = imgLine;
    style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    LKLockScanViewController *vc = [[LKLockScanViewController alloc] init];
    vc.style = style;
    vc.isOpenInterestRect = YES;
    vc.libraryType = SLT_Native;
    vc.scanType = LKScanTypeScanTicket;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
/// 创建视图
/// @param placeHolder 输入框占位符
-(UIView *)createHeaderViewWithTitle:(NSString *)placeHolder{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"收款金额" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentCenter];
    [bgView addSubview:headerLabel];
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(20);
        make.top.equalTo(bgView).offset(10);
    }];
    LKBaseLabel *moneyLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"￥" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:36] textAlignment:NSTextAlignmentCenter];
    [bgView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(20);
        make.top.equalTo(headerLabel.mas_bottom).offset(10);
    }];
    LKBaseLabel *yuanLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"元" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentRight];
    [bgView addSubview:yuanLabel];
    [yuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-20);
        make.centerY.equalTo(moneyLabel);
    }];
    self.moneyTf = [[UITextField alloc] init];
//    self.moneyTf.xmKeyBoardType = XMkeyBoardType_NumberStyleOne;
    self.moneyTf.placeholder = placeHolder;
    self.moneyTf.textAlignment = NSTextAlignmentLeft;
    self.moneyTf.keyboardType = UIKeyboardTypeDecimalPad;
    [self.moneyTf becomeFirstResponder];
    LKCustomKeyBoardView *boradView = [[LKCustomKeyBoardView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH) AndreturnTitle:@"收款"];
    boradView.delegate = self;
    self.moneyTf.inputView = boradView;
    [bgView addSubview:self.moneyTf];
    [self.moneyTf mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(moneyLabel.mas_right).offset(20);
       make.right.equalTo(yuanLabel.mas_left).offset(-20);
       make.centerY.equalTo(moneyLabel);
    }];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = viewDefualtColor;
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(20);
        make.right.equalTo(bgView.mas_right).offset(-20);
        make.top.equalTo(moneyLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    }];
    return bgView;
}

/// 创建备注view
/// @param note 备注描述
-(UIView *)createNoteViewWithTitle:(NSString *)note{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"备注" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentLeft];
    [bgView addSubview:headerLabel];
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(20);
        make.centerY.equalTo(bgView);
    }];
    self.noteTf = [[UITextField alloc] init];
    self.noteTf.placeholder = note;
    self.noteTf.textAlignment = NSTextAlignmentLeft;
    self.moneyTf.keyboardType = UIKeyboardTypeDecimalPad;
    [bgView addSubview:self.noteTf];
    [self.noteTf mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(bgView.mas_right).offset(-20);
       make.left.equalTo(headerLabel.mas_right).offset(20);

       make.centerY.equalTo(bgView);
    }];
    return bgView;
}
#pragma mark -LKCustomKeyBoardViewDelegate

-(UIButton *)lkClickConten:(UIButton *)contenBtn{

    if ([self.moneyTf.text containsString:@"."]) {
        if ([[self.moneyTf.text componentsSeparatedByString:@"."] lastObject].length < 2) {
            self.moneyTf.text = [NSString stringWithFormat:@"%@%@",self.moneyTf.text,contenBtn.currentTitle];
        }else{
            return contenBtn;
        }
    }else{
        if (self.moneyTf.text.length > 4) {
            if ([contenBtn.currentTitle isEqualToString:@"."]) {
                self.moneyTf.text = [NSString stringWithFormat:@"%@%@",self.moneyTf.text,contenBtn.currentTitle];
            }else{
                return contenBtn;
            }
        }else{
            self.moneyTf.text = [NSString stringWithFormat:@"%@%@",self.moneyTf.text,contenBtn.currentTitle];
        }
    }
    return contenBtn;
}
-(UIButton *)lkClickDelete:(UIButton *)deleteBtn{
    [self.moneyTf deleteBackward];
    return deleteBtn;
}
-(UIButton *)lkClickClear:(UIButton *)clearBtn{
    self.moneyTf.text = @"";
    return clearBtn;
}
-(UIButton *)lkClickdetermine:(UIButton *)determineBtn{
    NSLog(@"点击了%@",determineBtn.currentTitle);
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"jmt_remark":self.noteTf.text,@"total_amount":self.moneyTf.text,@"uid":[LKUserInforTool account].uid}];
    NSString *sign = [NSString encoingWithDic:param];
    [param setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyPayOpenUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {

        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            NSString *url = responseObject[@"data"][@"url"];
            NSString *order_sn = responseObject[@"data"][@"order_sn"];
            LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
            style.centerUpOffset = 44;
            style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
            style.photoframeLineW = 3;
            style.photoframeAngleW = 18;
            style.photoframeAngleH = 18;
            style.isNeedShowRetangle = NO;
            style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
            //qq里面的线条图片
            UIImage *imgLine = [UIImage imageNamed:@"qrcode_scan_light_green"];
            style.animationImage = imgLine;
            style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
            LKLockScanViewController *vc = [[LKLockScanViewController alloc] init];
            vc.style = style;
            vc.isOpenInterestRect = YES;
            vc.libraryType = SLT_Native;
            vc.order_sn = order_sn;
            vc.url = url;
            vc.price = self.moneyTf.text;
            vc.jmt_remark = self.noteTf.text;
            vc.scanType = LKScanTypeCashier;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }

    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
    return determineBtn;
}
//  键盘弹出触发该方法
- (void)keyboardDidShow:(NSNotification *)aNotification {
    NSLog(@"键盘弹出");
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    NSLog(@"键盘高度；%d",height);
}
//  键盘隐藏触发该方法
- (void)keyboardDidHide:(NSNotification *)aNotification {
    NSLog(@"键盘隐藏");
}

/// 限制输入只能是数字
/// @param string 字符串
- (BOOL)onlyInputTheNumber:(NSString*)string{
    NSString *numString =@"[0-9]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numString];
    BOOL inputString = [predicate evaluateWithObject:string];
    return inputString;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
